class Entry < ActiveRecord::Base
    include Aggregator::EntryModel
        
    has_many :marked_entries
    
    def self.get_entries_for_aggregation(aggregation_id, user_id, options = {})
        sql = "SELECT entries.*, feeds.service_id, feeds.title AS feed_title, feeds.uri AS feed_uri, entries_users.user_id IS NULL AS unread "
        sql << "FROM entries "
        sql << "INNER JOIN feeds on feeds.id = entries.feed_id "
        sql << "INNER JOIN aggregation_feeds ON aggregation_feeds.feed_id = feeds.id "
        sql << "LEFT OUTER JOIN entries_users ON entries_users.entry_id = entries.id AND entries_users.user_id = ? "       
        if aggregation_id <= 0
            user_aggregations = Aggregation.find(:all, :conditions => ['aggregations.user_id = ?', user_id])
            sql << "WHERE aggregation_feeds.aggregation_id IN (" + user_aggregations.collect{|agg| agg.id}.join(',') + ") "
        else
            sql << "WHERE aggregation_feeds.aggregation_id = ? "
        end
        sql << "AND " + exclude_sql
        sql << "ORDER By entries.published_at DESC "
        add_limit!(sql, options)
        args = [sql, user_id]
        args.push(aggregation_id) if aggregation_id > 0
        Entry.find_by_sql(args)
    end
    
    def self.get_bookmarks_for_aggregation(aggregation_id, user_id, options = {})
        sql = "SELECT entries.*, feeds.service_id, feeds.title AS feed_title, feeds.uri AS feed_uri "
        sql << "FROM entries "
        sql << "INNER JOIN feeds on feeds.id = entries.feed_id "
        sql << "INNER JOIN aggregation_feeds ON aggregation_feeds.feed_id = feeds.id "
        if aggregation_id <= 0
            user_aggregations = Aggregation.find(:all, :conditions => ['aggregations.user_id = ?', user_id])
            sql << "WHERE aggregation_feeds.aggregation_id IN (" + user_aggregations.collect{|agg| agg.id}.join(',') + ") "
        else
            sql << "WHERE aggregation_feeds.aggregation_id = ? "
        end
        sql << " AND feeds.service_id = " + Aggregator::ServiceConstants::DELICIOUS.to_s
        sql << "ORDER By entries.published_at DESC "
        add_limit!(sql, options)
        args = [sql]
        if aggregation_id > 0
            args.push(aggregation_id)
        end
        Entry.find_by_sql(args)
    end
              
    def self.get_entries_for_user(user_id, options = {})        
        sql = "SELECT entries.*, feeds.service_id, feeds.title AS feed_title, feeds.uri AS feed_uri, entries_users.user_id IS NULL AS unread "
        sql << "FROM entries "
        sql << "INNER JOIN feeds on feeds.id = entries.feed_id "
        sql << "INNER JOIN aggregation_feeds ON aggregation_feeds.feed_id = feeds.id "
        sql << "LEFT OUTER JOIN entries_users ON entries_users.entry_id = entries.id AND entries_users.user_id = ? "
        sql << "INNER JOIN aggregations ON aggregations.id = aggregation_feeds.aggregation_id "
        sql << "WHERE aggregations.user_id = ? "
        sql << "AND " + exclude_sql
        sql << "ORDER By entries.published_at DESC "
        add_limit!(sql, options)
        args = [sql, user_id, user_id]
        Entry.find_by_sql(args)
    end
    
    def self.get_entries_read_for_user(user_id, options = {})        
        sql = "SELECT entries.*, feeds.service_id, feeds.title AS feed_title, feeds.uri AS feed_uri, 0 AS unread "
        sql << "FROM entries "
        sql << "INNER JOIN feeds on feeds.id = entries.feed_id "
        sql << "INNER JOIN entries_users ON entries_users.entry_id = entries.id AND entries_users.user_id = ? "
        sql << "WHERE " + exclude_sql
        sql << "ORDER By entries.published_at DESC "
        add_limit!(sql, options)
        args = [sql, user_id]
        Entry.find_by_sql(args)
    end
        
    def self.get_marked_entries_for_user(user_id, options = {})        
        sql = "SELECT entries.*, feeds.service_id, feeds.title AS feed_title, feeds.uri AS feed_uri, 0 AS unread "
        sql << "FROM entries "
        sql << "INNER JOIN feeds on feeds.id = entries.feed_id "
        sql << "INNER JOIN marked_entries ON marked_entries.entry_id = entries.id AND marked_entries.user_id = ? "
        sql << "WHERE " + exclude_sql
        sql << "ORDER By entries.published_at DESC "
        add_limit!(sql, options)
        args = [sql, user_id]
        Entry.find_by_sql(args)
    end
    
    def self.get_marked_entries_for_aggregation_and_user(aggregation_id, user_id, options = {})        
        sql = "SELECT entries.*, feeds.service_id, feeds.title AS feed_title, feeds.uri AS feed_uri, 0 AS unread "
        sql << "FROM entries "
        sql << "INNER JOIN feeds on feeds.id = entries.feed_id "
        sql << "INNER JOIN aggregation_feeds ON aggregation_feeds.feed_id = feeds.id "
        sql << "INNER JOIN marked_entries ON marked_entries.entry_id = entries.id AND marked_entries.user_id = ? "
        sql << "WHERE " + exclude_sql
        sql << " AND aggregation_feeds.aggregation_id = ? "
        sql << "ORDER By entries.published_at DESC "
        add_limit!(sql, options)
        args = [sql, user_id, aggregation_id]
        Entry.find_by_sql(args)
    end
    
    protected
    
    def self.exclude_sql
        sql = "feeds.service_id != " + Aggregator::ServiceConstants::FLICKR.to_s # don't include flickr.  It will show up in photos"
        sql << " AND feeds.service_id != " + Aggregator::ServiceConstants::DELICIOUS.to_s # don't include delicious.  It will show up in bookmarks"
        sql << " "
    end
                    
end